GBML manual / Stacks

stack_create

Makes an empty stack that can hold up to capacity items. example is any value of the kind it will hold - stack_create(16, 0) is a stack - and is never put in the stack. capacity must be a whole number written right there in the code.

Syntax

stack_create(capacity, example) -> stack<T>

Arguments

NameType
capacityint
exampleelement

Returns

stack<T>

Description

Makes an empty stack that can hold up to capacity items. stack_create(16, 0) is a stack of whole numbers with room for 16.

capacity is the most the stack will ever hold. It has to be a number you type, like 16, not a variable, because a GBA cannot hand out memory while a game runs. A stack can be any size you like.

example tells GBML what kind of thing goes on the stack. Give any value of that kind: 0 for whole numbers, 0.0 for decimals, "" for text, true for yes-or-no. The stack starts empty, and the example is never pushed onto it. It is only read to learn the kind.

It sets the size of the variable it is given to, so it goes on the right-hand side of the line that creates the variable: var undo = stack_create(16, 0);.

Leave the size out and you get 8.

stack_create(16, 0) means the same as var undo: stack<int, 16> = []; and the /// @capacity 16 comment. See list_create for the three ways side by side.

Example

var undo = stack_create(16, 0);     // an empty stack of whole numbers
stack_push(undo, 4);
stack_push(undo, 9);
var last = stack_pop(undo);         // 9 - the last one in is the first one out

On the GBA

Compiles to an empty fixed-size vector of `capacity` items, the same storage `/// @type {stack<T>}` + `/// @capacity capacity` gives. Leave the size out and a stack holds 8. The size has to be a plain number because it becomes a C++ template parameter: the Game Boy Advance has no heap to hand out memory while the game runs, so every container is sized when the ROM is built (anything else is refused). It must be the whole right-hand side of a declaration, because that is the one place a size can be attached to a variable. The example is only read for its kind - it is never evaluated, never stored and never put in the container.

See also: stack_pushstack_popstack_copylist_createqueue_createStacksWriting a type out, instead of in a commentData containers